<template>
  <el-row style="height: 100vh">
    <!-- <el-col :xs="8" :md="12" :lg="9" :span="16" class="left">
      <h1>千峰-贵阳后台管理系统</h1>
      <h3>欢迎您来到后台管理系统，请输入相关信息，进入后台</h3>
    </el-col> -->
    <!-- :xs="16" :md="12" :lg="15" :span="8"  -->

    <el-col class="right">
      <div class="lss">
        <p>登录</p>
        <el-form
          ref="formRef"
          :model="formInline"
          :rules="rules"
          class="demo-form-inline"
          id="haha"
        >
          <el-form-item label="用户名：" prop="username" class="coo">
            <el-input v-model="formInline.username" placeholder="请输入用户名">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="密&nbsp;&nbsp;&nbsp;码：" prop="password">
            <el-input
              v-model="formInline.password"
              type="password"
              show-password
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loadingLOgin"
              type="primary"
              style="width: 100%"
              @click="onSubmit"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { login } from "../api/manager.js";
import { toast } from "../utils/index.js";
import { setCookie } from "../utils/auth.js";
import { useAdminStore } from "../stores/admin.js";
import { ElNotification } from "element-plus";
const router = useRouter();

const store = useAdminStore();
const loadingLOgin = ref(false);
const formInline = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [
    {
      required: true,
      message: "请输入正确的用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入正确的密码",
      trigger: "blur",
    },
  ],
});
const formRef = ref(null);

const onSubmit = () => {
  console.log("???");
  formRef.value.validate((res) => {
    // 校验后结果
    console.log(res);
    if (!res) {
      return false;
    }
    login(formInline.username, formInline.password)
      .then((res) => {
        loadingLOgin.value = true;
        // 登录成功提示
        toast("登录成功");
        console.log(res);
        // ElNotification({
        //   message:"登录成功",
        //   type:"success",
        //   duration:2000,
        // })
        // 拿token里面的值
        const token = res.token;
        // console.log(token);
        setCookie(token);
        // 获取用户数据
        store.getAdminInfo();

        // 跳转到首页
        router.push({
          path: "/",
        });
      })
      .catch((err) => {
        // 登录失败提示
        console.log(err, "error");
        // toast("您的账号或者密码不正确哦！请再检查一遍", "error");
        // ElNotification({
        //   message:"登录失败",
        //   type:"error",
        //   duration:2000,
        // })
      })
      .finally(() => {
        loadingLOgin.value = false;
      });
  });
};
// 设置按键盘回车事件进行登录
function keyUP(event) {
  // console.log(event);
  if (event.keyCode === 13) onSubmit();
}
onMounted(() => {
  window.addEventListener("keyup", keyUP);
});
onUnmounted(() => {
  window.removeEventListener("keyup", keyUP);
});
</script>
<style scoped>
.left {
  background: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202005%2F06%2F20200506200732_nenqt.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671586792&t=1498e3f5ee925bc7c107e9ef2c9a151a")
    0 0 no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  color: #fff;
}
.right {
  background: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-size: 30px;
  background: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13945121732%2F1000&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671602507&t=9cd81cdada0caaa1eacb74776eca3e8e")
    no-repeat;
  background-size: 100% 100%;
  /* background-color: aqua; */
}
.right p {
  text-align: center;
  font-size: 40px;
  color: #fff;
}
.right > .lss {
  background-color: rgb(2, 31, 31);
  padding: 0px 30px;
}
</style>
